import Taro, { Component } from '@tarojs/taro'
import { View, Text, Button } from '@tarojs/components'
import Base from '@/components/Base'
import { ToastUtil } from '../../utils/toastUtil.js'
import { getInfo, getWxInfo } from '@/utils/api';

class Index extends Component {
  config = {
    navigationBarTitleText: ''
  }

  state = {
    username: '',
    role: '',
    wxUserInfo: ''
  }
  componentWillUnmount() {
    this.clearTimer()
  }

  componentDidHide() {
    this.clearTimer()
  }

  clearTimer() {
    this.timer && clearInterval(this.timer)
  }

  showToast() {
    ToastUtil.info('66666')
  }

  getUserInfo() {
    getInfo().then(res => {
      const { data: { username, role } } = res
      this.setState({ username, role })
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  }

  tastGetUserInfo() {
    this.timer && clearInterval(this.timer)
    this.timer = setInterval(() => {
      this.getUserInfo()
    }, 1000)
  }

  getWxInfo() {
    getWxInfo().then(res => {
      this.setState({ wxUserInfo: res.userInfo || '' })
    }).catch(err => {
      console.log(err)
      Taro.navigateTo({
        url: '/pages/get-wx-auth/index'
      })
    })
  }

  render() {
    return (
      <Base>
        <View>
          用户名: <Text>{this.state.username}</Text>
          角色: <Text>{this.state.role}</Text>
          微信用户信息: <Text>{this.state.wxUserInfo}</Text>
        </View>
        {/* <Button onClick={this.showToast} >显示按钮</Button> */}
        <Button onClick={this.getUserInfo}>获取用户信息</Button>
        <Button onClick={this.tastGetUserInfo}>定时获取用户信息</Button>
        <Button onClick={this.getWxInfo}>微信用户信息</Button>
        <Button onClick={this.showToast}>showToast</Button>
      </Base>
    )
  }
}
export default Index
